<?php
class ControllerMarketingMessage extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('marketing/message');

		$this->document->setTitle($this->language->get('heading_title'));

		$data['user_token'] = $this->session->data['user_token'];

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('marketing/message', 'user_token=' . $this->session->data['user_token'], true)
		);

		$data['cancel'] = $this->url->link('marketing/message', 'user_token=' . $this->session->data['user_token'], true);

		$this->load->model('setting/store');
		//购物车加提示
        $this->load->model('marketing/message');
		$data['stores'] = $this->model_setting_store->getStores();
		$data['message'] = $this->model_marketing_message->getMessage();
		$this->load->model('customer/customer_group');

		$data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
        $data['action'] = $this->url->link('marketing/message/edit', 'user_token=' . $this->session->data['user_token'], true);
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('marketing/message', $data));
	}
    
	
	
	public function edit() {
		$this->load->language('marketing/message');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('marketing/message');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_marketing_message->editMessage($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('marketing/message', 'user_token=' . $this->session->data['user_token'] . $url, true));
		}

		// $this->getForm();
	}

	protected function validateForm() {
		if (!$this->user->hasPermission('modify', 'marketing/message')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		// if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 128)) {
			// $this->error['name'] = $this->language->get('error_name');
		// }

		// if ((utf8_strlen($this->request->post['code']) < 3) || (utf8_strlen($this->request->post['code']) > 10)) {
			// $this->error['code'] = $this->language->get('error_code');
		// }

		// $coupon_info = $this->model_marketing_coupon->getCouponByCode($this->request->post['code']);

		// if ($coupon_info) {
			// if (!isset($this->request->get['coupon_id'])) {
				// $this->error['warning'] = $this->language->get('error_exists');
			// } elseif ($coupon_info['coupon_id'] != $this->request->get['coupon_id']) {
				// $this->error['warning'] = $this->language->get('error_exists');
			// }
		// }

		return !$this->error;
	}

}
